/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *              C E D A R
 *          S O L U T I O N S       "Software done right."
 *           S O F T W A R E
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Copyright (c) 2011 Kenneth J. Pronovici.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0.
 * See doc/LICENSE for more information about the licensing terms.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Author   : Kenneth J. Pronovici <pronovic@ieee.org>
 * Language : Java 6
 * Project  : Secret Santa Exchange
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package com.cedarsolutions.santa.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.mvp4g.client.event.Mvp4gLogger;

/**
 * Custom Mvp4g logger with a couple of different modes.
 *
 * <p>
 * The logger can either write to the GWT log, or can pop an alert
 * for each message.  It's configured via the SantaExchangeConfig
 * constants bundle.
 * </p>
 *
 * @author Kenneth J. Pronovici <pronovic@ieee.org>
 */
public class SantaExchangeCustomLogger implements Mvp4gLogger {

    /** Disables logging. */
    public static final String DISABLED = "disabled";

    /** Configures alert logging mode. */
    public static final String ALERT_MODE = "alert";

    /** Configures GWT logging mode. */
    public static final String GWT_MODE = "gwt";

    /** Configured alert mode. */
    private String loggingMode;

    /** Configured logging prefix. */
    private String loggingPrefix;

    /** Create a custom logger. */
    public SantaExchangeCustomLogger() {
        SantaExchangeConfig config = GWT.create(SantaExchangeConfig.class);  // note: can't be injected 'cause Gin doesn't create this class
        this.loggingMode = config.customLogger_loggingMode();
        this.loggingPrefix = config.customLogger_loggingPrefix();
    }

    /** Log a message. */
    @Override
    public void log(String message, int depth) {
        if (ALERT_MODE.equals(this.loggingMode)) {
            Window.alert(message);
        } else if (GWT_MODE.equals(this.loggingMode)) {
            GWT.log(loggingPrefix + generatePadding(depth) + message);
        }
    }

    /** Generate padding based on a depth. */
    private static String generatePadding(int depth) {
        final String tabstop = "    ";

        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < depth; i++) {
            buffer.append(tabstop);
        }

        return buffer.toString();
    }

    /** Get the configured logging mode. */
    public String getLoggingMode() {
        return this.loggingMode;
    }

    /** Get the configured logging prefix. */
    public String getLoggingPrefix() {
        return this.loggingPrefix;
    }

}
